package com.wythe.mall.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.hb.dialog.dialog.ConfirmDialog;
import com.wythe.mall.R;
import com.wythe.mall.activity.AddressActivity;
import com.wythe.mall.activity.CenterActivity;
import com.wythe.mall.activity.LoginActivity;
import com.wythe.mall.activity.OrderActivity;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.model.UserBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;
import com.wythe.mall.utils.UserManager;
import com.wythe.mall.view.CenterTextView;
import com.wythe.mall.view.NoScrollGridView;
import com.wythe.mall.view.PersonalItemTitle;
import com.wythe.mall.view.PersonalOrderItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class PersonalFragment extends Fragment implements View.OnClickListener {

    private View view;
    private CircleImageView mAvatarImg;
    private TextView mNickName;
    private RelativeLayout personal_for_login_info;
    private NoScrollGridView mGridView;
    private ImageShopAdapter mShopAdapter;
    private List<ShopBean> list;
    private CenterTextView mCenterTitle;
    private PersonalItemTitle personal_order_title;
    private LinearLayout personalSet;
    private int shopId;
    private PersonalOrderItem mPendingPay,mDelivered,mReceived,mCompleted;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_personal, container, false);
        initView();
        return view;
    }

    private void initData() {
        JSONObject param_last = new JSONObject();
        try {
            param_last.put("position",3);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.getClassLabel(getActivity(),param_last, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (code == 200) {
                    try {
                        JSONArray jsonArray = data.getJSONArray("data");
                        shopId = jsonArray.getJSONObject(0).getInt("id");
                        String title = jsonArray.getJSONObject(0).getString("title");
                        mCenterTitle.setText(title);
                        getShopList(shopId);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    private void initView() {
        mAvatarImg = view.findViewById(R.id.user_img_view);
        mNickName = view.findViewById(R.id.tv_name);
        personalSet = view.findViewById(R.id.personal_set);
        mPendingPay = view.findViewById(R.id.personal_order_item_1);
        mDelivered = view.findViewById(R.id.personal_order_item_2);
        mReceived = view.findViewById(R.id.personal_order_item_3);
        mCompleted = view.findViewById(R.id.personal_order_item_4);
        mPendingPay.setOnClickListener(this);
        mDelivered.setOnClickListener(this);
        mReceived.setOnClickListener(this);
        mCompleted.setOnClickListener(this);
        personalSet.setOnClickListener(this);
        personal_for_login_info = view.findViewById(R.id.personal_for_login_info);
        personal_order_title = view.findViewById(R.id.personal_order_title);
        personal_order_title.setOnClickListener(this);
        mGridView = view.findViewById(R.id.grid_image);
        mCenterTitle = view.findViewById(R.id.ctv_title);
        mShopAdapter = new ImageShopAdapter(getActivity(), list);
        mGridView.setAdapter(mShopAdapter);
        personal_for_login_info.setOnClickListener(this);
        String name = SharePreferenceUtils.getString(getActivity(), "NAME");
        String avatar = SharePreferenceUtils.getString(getActivity(), "AVATAR");
        if (name != null && avatar != null) {
            Glide.with(getActivity()).load(avatar).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(mAvatarImg);
            mNickName.setText(name);
        }
        view.findViewById(R.id.personal_guanzhu).setOnClickListener(this);
        view.findViewById(R.id.personal_zuji).setOnClickListener(this);
        view.findViewById(R.id.personal_hongbao).setOnClickListener(this);
        view.findViewById(R.id.personal_feedback).setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        String name = SharePreferenceUtils.getString(getActivity(), "NAME");
        String avatar = SharePreferenceUtils.getString(getActivity(), "AVATAR");
        if (name != null && avatar != null) {
            Glide.with(getActivity()).load(avatar).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(mAvatarImg);
            mNickName.setText(name);
        }
        initData();
    }

    public void getShopList(int type) {
        JSONObject params = new JSONObject();
        try {
            params.put("indexLabelId", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getShopList(getActivity(), params, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.clear();
                try {
                    JSONArray array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ShopBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ShopBean.class);
                        list.add(bean);
                    }
                    mShopAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.personal_for_login_info: {
                if (!UserManager.getInstance().isLogin()) {
                    GotoActivity.gotoActiviy(getActivity(), LoginActivity.class);
                }
                break;
            }
            case R.id.personal_guanzhu: {
                ConfirmDialog confirmDialog = new ConfirmDialog(getActivity());
                confirmDialog.setLogoImg(R.mipmap.ic_launcher).setMsg("商家电话：16666666666");
                confirmDialog.setClickListener(new ConfirmDialog.OnBtnClickListener() {
                    @SuppressLint("MissingPermission")
                    @Override
                    public void ok() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "16666666666"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }

                    @Override
                    public void cancel() {

                    }
                });
                confirmDialog.show();
                break;
            }
            case R.id.personal_zuji: {
                Bundle bundle = new Bundle();
                GotoActivity.gotoActiviy(getActivity(), AddressActivity.class,bundle);
                break;
            }
            case R.id.personal_hongbao:
                break;
            case R.id.personal_feedback:

                break;

            case R.id.personal_order_title:
                GotoActivity.gotoActiviy(getActivity(), OrderActivity.class);
                break;

            case R.id.personal_set:
                GotoActivity.gotoActiviy(getActivity(), CenterActivity.class);
                break;

            case R.id.personal_order_item_1:
                Bundle bundle1 = new Bundle();
                bundle1.putInt("id",1);
                GotoActivity.gotoActiviy(getActivity(), OrderActivity.class,bundle1);
                break;
            case R.id.personal_order_item_2:
                Bundle bundle2 = new Bundle();
                bundle2.putInt("id",2);
                GotoActivity.gotoActiviy(getActivity(), OrderActivity.class,bundle2);
                break;
            case R.id.personal_order_item_3:
                Bundle bundle3 = new Bundle();
                bundle3.putInt("id",3);
                GotoActivity.gotoActiviy(getActivity(), OrderActivity.class,bundle3);
                break;
            case R.id.personal_order_item_4:
                Bundle bundle4 = new Bundle();
                bundle4.putInt("id",4);
                GotoActivity.gotoActiviy(getActivity(), OrderActivity.class,bundle4);
                break;
        }
    }
}
